package com.whately.core.base.utils;

import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class GsonIntegerTypeAdapter extends TypeAdapter<Number> {
    @Override
    public void write(JsonWriter out, Number value) throws IOException {
        //out.value(value);
        if (value == null) {
            out.nullValue();
        } else {
            if (value instanceof Long) {
                out.value(value.longValue());
            } else {
                out.value(value.intValue());
            }
        }
    }

    @Override
    public Number read(JsonReader in) throws IOException {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return null;
        } else {
            try {
                return in.nextLong();
            } catch (NumberFormatException var3) {
                throw new JsonSyntaxException(var3);
            }
        }
    }
}
